<?php
/**
* Contains MySQL connection class
* @package WebIntegrator_main
*/

/**
* MySQL connection class
*
*   Usage:
* <code>
* $CONN = new Connection() ;
* $CONN->set(DB_HOST, DB_USER, 'c6JQ7', 
*	'3307', '/tmp/mysql4.sock' , 'testing') ;
* $CONN->reconnect() ;
* $CONN->get_databases() ;
* $CONN->begin() ;
* echo $CONN->show_progress() ;
* </code>
* @package WebIntegrator_main
*/

class Connection {

    var $id							= false ;
    var $user						= '' ;
    var $password				= '' ;
    var $host						= '' ;
    var $port						= '' ;
    var $socket					= '' ;
    var $db							= '' ;

	var $crpt						= _DBCRYPT ;

	var $transaction_autostart	= false ;
	var $persistent_linkage		= true ;
    
	var $query			= '' ;
	var $query_type	= NULL ;

	var $result					= NULL ;
    var $data						= NULL ;
    var $rows						= NULL ;
    var $affected_rows		= NULL ;
    var $last_insert_id		= NULL ;

	var $log = array() ;
	var $error ;
	
	var $tr_close = true ;
	var $tr_started = false ;

	function Connection() {
		$this->set() ;
		$this->reconnect() ;
	} //function

	function set($host = _DBHOST, $user = _DBUSER, $password = _DBPASS, $port = _DBPORT, $socket = _DBSOCKET, $db = _DBNAME, $persistent_linkage = false) {
		$this->user = $user ;
		$this->password = $password ;
		$this->host = $host ;
		$this->port = $port ;
		$this->socket = $socket ;
		if($this->port) $this->host .= ":" . $this->port;
        if($this->socket) $this->host .= ":" . $this->socket;
		$this->db = $db ;
		$this->persistent_linkage = $persistent_linkage ;
	} //function

	function reconnect() {
		$conn = $this->persistent_linkage ? 'mysql_pconnect' : 'mysql_connect' ;
		$this->id = @$conn($this->host, $this->user, $this->password) ;
		$this->record_progress('Connecting to ' . $this->host .' ' . $this->user .' ' . $this->password) ;
		$this->record_progress($this->id ? 'Success' : $this->get_error()) ;
		$this->set_database() ;
	} //function

	function set_database() {
		@mysql_select_db($this->db) ;
		$this->record_progress('Using database ' . $this->db) ;
		$this->record_progress($this->get_error()) ;
	} //function

	function query($query) {
		
		$this->clear() ;
		$this->query = $query ;
		$this->record_progress($this->query) ;
		$this->result = @mysql_query($query,$this->id) ;
		$res = $this->get_error() ;
		$msg = '' ;
		$this->query_type = $this->_get_query_type($this->query) ;
		switch($this->query_type) {
			case 'select' :
				$this->rows = @mysql_num_rows($this->result) ;
				$msg = ' - rows: ' . (int)$this->rows ;
					break ;
			case 'insert':
				$this->affected_rows = @mysql_affected_rows($this->id) ;
				$this->get_last_insert_id() ;
				$msg = ' - affected rows: ' . (int)$this->affected_rows . ', - last insert id: ' . $this->last_insert_id ;
					break;
			case 'delete':
			case 'update':
				$this->affected_rows = @mysql_affected_rows($this->id) ;
				$msg = ' - affected rows: ' . (int)$this->affected_rows ;
				break;
			default :
					break ;
		} //switch
		$this->record_progress($res . $msg) ;

		if($res != 'Success') {
			return $res;
		} //i f
			return true;
	} //function

	function _get_query_type($query) {
		$query = trim($query) ;
		if(eregi('^SELECT',$query)) {
			return 'select' ;
		} //if
		elseif(eregi('^(INSERT|REPLACE)',$query)) {
			return 'insert' ;
		} //if
		elseif(eregi('^(DELETE|TRUNCATE)',$query)) {
			return 'delete' ;
		} //if
		elseif(eregi('^UPDATE',$query)) {
			return 'update' ;
		} //if
		else {
			return 'other' ;
		} //else
	} //function

	function begin() {
		if(!$this->tr_started) @mysql_query('BEGIN',$this->id) ;
		else return;
		$this->tr_started = true;
		$this->tr_close = false;
		$this->record_progress('TRANSACTION STARTED') ;
	} //function

	function commit() {
		if($this->tr_started) @mysql_query('COMMIT',$this->id) ;
		else return;
		$this->record_progress('TRANSACTION COMMITTED') ;
		$this->tr_started = false;
		$this->tr_close = true;
	} //function

	function rollback() {
		if($this->tr_started) @mysql_query('ROLLBACK',$this->id) ;
		else return;		
		$this->record_progress('TRANSACTION ROLLED BACK') ;
		$this->tr_started = false;
		$this->tr_close = true;
	} //function

	function clear() {
		@mysql_free_result() ;
		$this->result = $this->affected_rows = $this->last_insert_id = $this->rows = false ;
	} //function

	function fetch() {
		$this->data = mysql_fetch_row($this->result) ;
	} //function

	function fetch_object() {
		$this->data = mysql_fetch_object($this->result) ;
	} //function

	function get_single($str) {
		$this->query($str) ; $data = array() ;
		if(!$this->rows) return $data ;		
		$this->fetch();
		for ( $j=0 ; $j < count($this->data) ; $j++ ) {
			$data[$j] = $this->data[$j];
		} // for
		return $data ;
	} //function

	function get_list($str) {
		$this->query($str) ; $data = array() ;
		if(!$this->rows) return $data ;		
		for ( $i=0 ; $i < $this->rows ; $i++ ) {
			$this->Fetch();
			for ( $j=0 ; $j < count($this->data); $j++ ) {
				$data[$i][] = $this->data[$j];
			} // for
		} // for
		return $data ; 
	} //function

	function get_mod($str) {
		$res = $this->query($str) ; $data = array() ;
			$data[rows] = $this->affected_rows ;
			$data[last_id] = $this->last_insert_id ? $this->last_insert_id : NULL ;
			$data[error] = $res !== true ? $res : false ;
		return $data ;
	} //function

	function get_last_insert_id() {
		$this->last_insert_id = @mysql_insert_id($this->id) ;
	} //function

	function record_progress($msg) {
		$this->log[] = $msg ;
	} //function

	function show_progress() {
		return '<div style="font-family: Verdana; font-size: 8pt;">'
			. implode('<br>'."\n",$this->log) . '</div>' ;
	} //function

	function get_error() {
		$e = @mysql_error($this->id) ;
		if($e) $this->error[] = $e ;
		return $e ? $e : 'Success' ;
	} //function

	/*------------------------------ SERVICE FUNCTIONS --------------------------------------------*/

    function get_databases() {
        if($result=mysql_list_dbs($this->id)){
            $i=0;
            while($i<mysql_num_rows($result)){
                $this->databases[$this->host][]=mysql_tablename($result,$i++);
            } // while
			return TRUE;
        } // if
		else {
			$this->record_progress("Found no databases in " . $this->host) ;
			return FALSE;
		}
    } //function

	 function create_database($database){
        if($result=mysql_create_db($database,$this->id)){
            return TRUE;
        }
		else {
           $this->record_progress("Unable to create database $database") ;
		   $this->record_progress($this->get_error()) ; 
			return FALSE;
		}
    }

    function drop_database($database){
        if($result=mysql_drop_db($database)){
            return TRUE;
        }
		else {
           $this->record_progress("Unable to drop database $database") ;
		   $this->record_progress($this->get_error()) ; 
		   return FALSE;	
		} // else
    }

    function get_tables(){
        if($result=mysql_list_tables($this->db,$this->id)){
            $i=0;
            while($i < mysql_num_rows($result)){
                $this->table_names[$i]=mysql_tablename($result,$i++);
            }
			 return TRUE;
        }
		else {
			$this->record_progress("Cannot get tables from ". $this->db);	
			$this->record_progress($this->get_error()) ; 
			return FALSE;	
		} // else
    }

    function get_table_fields_list($tbl_name){
        if($result=mysql_list_fields($this->db,$tbl_name,$this->id)){
            $i=0;
            while($i < mysql_num_fields($result)){
                $this->tables_list[$tbl_name][]=mysql_field_name($result,$i++);
            }
            return TRUE;
        }
		else {
			$this->record_progress("Cannot get fields from ". $tbl_name);	
			$this->record_progress($this->get_error()) ; 
			return FALSE;	
		} // else
    }

	function get_cursor_fields_list(){
        if(!$this->result) {
        	$this->record_progress("Cannot get fields from current result");	
			$this->record_progress($this->get_error()) ; 
			return FALSE;	
        } // if
		else {
			$lst = array();
			while($i < mysql_num_fields($this->result)){
                $lst[]=mysql_field_name($this->result,$i++);
			}//while
				return $lst ;
            }// else
		} 

} //class

  $CONN = new Connection() ;
 //debug($CONN);
?>